/**
 * 优化 asyncData 时的请求速度，同时发起多个请求，所有请求结束后回调
 * @param {Array} list 请求列表数据 { fn: 请求函数, params: 请求参数, cb: then回调, error: catch回调 }
 */
export function requestList (list) {
  const that = this
  return new Promise((resolve, reject) => {
    if (!list || !list.length) resolve()

    let len = list.length

    list.forEach(({ fn, params = {}, cb, error }) => {
      fn && fn.call(that, params).then(res => {
        cb && cb(res)

        len -= 1
        if (len <= 0) resolve()
      }).catch(e => {
        console.log('error: ', e)
        error = error || console.error
        error(e)

        len -= 1
        if (len <= 0) resolve()
      })
    })
    // end
  })
}
